<template>
	<view class="wp">

		<view class="header">
			<view class="txt-xh">
				<view class="h3" v-if="isLogin">
					¥{{userInfo}}
				</view>
				<view class="h3" v-else>
					未登录
				</view>
				<text v-if="isLogin">可提现金额(元)</text>
				<text v-else>请先登录查看收益</text>
				<text class="s2" v-if="isLogin">?</text>
			</view>
			<!-- 添加提现记录按钮 -->
			<view class="withdraw-record" @click="getWrite" v-if="isLogin">
				<uni-icons type="list" size="16" color="#fff"></uni-icons>
				<text>提现记录</text>
			</view>
		</view>
		<view class="boe">
			<view class="m-about" :class="{ 'not-login': !isLogin }">
				<template v-if="isLogin">
					<view class="h3">
						提现金额
					</view>
					<view class="num">
						<view class="desc">￥</view> <text>每日最高提现</text>
						<input class="inp" v-model="paynums" placeholder="请输出金额" type="number">
					</view>
					<!-- 添加金额选项 -->
					<view class="amount-options">
						<view class="amount-item" v-for="amount in amountOptions" :key="amount"
							:class="{ active: paynums == amount }" @click="selectAmount(amount)">
							<text>{{amount}}元</text>
						</view>
					</view>
					<view class="boe">
						<text>提现至</text>
						<text class="s1">微信零钱</text>
					</view>
					<view class="g-queding-kx">
						<view class="txt">
							<view class="s1" @click="Payouts">提现</view>
						</view>
					</view>
				</template>
				<template v-else>
					<view class="login-tip">
						<text>请先登录后查看收益详情</text>
						<button class="login-btn" @click="goToLogin">立即登录</button>
					</view>
				</template>
			</view>
		</view>
		<view class="main" v-if="isLogin">
			<view class="txt-xh1">
				<view class="tit">
					<view class="h3">
						说明：
					</view>
					<view class="info">
						1.<text>可提现金额</text>="每日受益"+"已完成的认养本金"
					</view>
					<view class="info">
						2.<text>每日受益</text>：每日累计可提现受益200元，提现的受益
						到微信零钱
					</view>
					<view class="info">
						3.<text>提现规则</text>：提现申请发起可在提现记录中查看状态
						提现审核通过后可点击领取到账微信零钱
					</view>
					<view class="info">
						4.<text>到账日期</text>：提现金额将在1-3个工作日内到账
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		setWithdraw,
		getUserDetail,
		getSetings
	} from "@/request/API.js";
	export default {
		data() {
			return {
				paynums: '',
				userInfo: {},
				amountOptions: [1, 5, 10, 30, 50, 100],
				isLogin: false
			}
		},
		onLoad(options) {
			this.checkLogin();
		},
		methods: {
			checkLogin() {
				const userId = uni.getStorageSync("userId");
				this.isLogin = !!userId;
				if (this.isLogin) {
					this.init();
				}
			},
			goToLogin() {
				uni.switchTab({
					url: "/pages/profile/profile"
				})
			},
			async init() {
				let id = uni.getStorageSync("userId")
				const {
					data
				} = await getUserDetail(id);
				this.userInfo = data.total;
			},
			getWrite() {
				uni.navigateTo({
					url: "/pages/withdraw/withdraw"
				})
			},
			Payouts() {
				let that = this;
				if (this.paynums <= 0) {
					uni.showToast({
						title: '请输入大于0的金额',
						icon: 'none'
					})
				} else if (this.paynums > 100) {
					uni.showToast({
						title: '最大提现金额为100',
						icon: 'none'
					})
				} else if (this.paynums > this.userInfo) {
					uni.showToast({
						title: '余额不足',
						icon: 'none'
					})
				} else {
					uni.showModal({
						title: '确认提现',
						content: `确定要提现${this.paynums}元吗？`,
						success: async (res) => {
							if (res.confirm) {
								await setWithdraw({
									userId: uni.getStorageSync("userId"),
									amount: that.paynums
								});
								uni.showToast({
									title: '提现待审核',
									icon: 'success'
								})
								that.init();
							}
						}
					})
				}
			},
			selectAmount(amount) {
				this.paynums = amount;
			}
		}
	}
</script>

<style>
	.wp {
		padding-top: 0;
	}

	.header {
		background-color: #1ec796;
		position: relative;
		height: 400rpx;
	}

	.txt-xh {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translateY(-50%) translateX(-50%);
	}

	.txt-xh .h3 {
		font-weight: bold;
		color: #fff;
		margin-bottom: 5rpx;
		font-size: 52rpx;
	}

	.txt-xh text {
		color: #fff;
		font-size: 24rpx;
	}

	.txt-xh .s2 {
		border: 1px solid #fff;
		border-radius: 50%;
		display: inline-block;
		margin-left: 15rpx;
		font-size: 14rpx;
		width: 30rpx;
		height: 30rpx;
		text-align: center;
		line-height: 30rpx;
	}

	.m-about {
		margin: 0 40rpx;
		padding: 20rpx 40rpx;
		margin-top: -80rpx;
		background-color: #fff;
		border-radius: 10rpx;
		position: relative;
		z-index: 99;
		box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
		margin-bottom: 140rpx;
	}

	.m-about .h3 {
		font-size: 28rpx;
		margin-bottom: 25rpx;
	}

	.m-about .num {
		margin-bottom: 35rpx;
		border-bottom: 1px solid #f2f2f2;
	}

	.m-about .num .desc {
		float: left;
		font-size: 24rpx;
		margin-bottom: 15rpx;
	}

	.m-about .num text {
		float: right;
		color: #09c18f;
		font-size: 24rpx;
		margin-bottom: 15rpx;
	}

	.m-about .boe {
		overflow: hidden;
		margin-bottom: 100rpx;
		color: #666666;
		font-size: 24rpx;
	}

	.m-about .boe text {
		float: left;
	}

	.m-about .boe .s1 {
		float: right;
	}

	uni-input {
		height: 1.4em;
		font-size: 24rpx;
		line-height: 1.4em;
		margin-bottom: 15rpx;
	}

	.g-queding-kx {
		text-align: center;
		background-color: #09c18f;
		height: 80rpx;
		line-height: 80rpx;
		font-size: 28rpx;
		border-radius: 10rpx;
		color: #fff;
		width: 100%;
	}

	.txt-xh1 {
		padding: 0 60rpx;
	}

	.txt-xh1 .h3 {
		display: block;
		font-size: 24rpx;
		margin-bottom: 20rpx;
	}

	.txt-xh1 .info {
		display: inline-block;
		font-size: 24rpx;
		letter-spacing: 1rpx;
		margin-bottom: 15rpx;
	}

	.txt-xh1 .info text {
		color: #000;
	}

	.txt-xh1 .info {
		color: #666666;

	}

	.txt-xh1 .desc {
		font-size: 28rpx;
		color: #1ec796;
		margin-bottom: 20rpx;
		margin-top: 5rpx;

	}

	.txt-xh1 .desc text {
		margin-right: 85rpx;

	}

	.withdraw-record {
		position: absolute;
		top: 20rpx;
		right: 20rpx;
		display: flex;
		align-items: center;
		background: rgba(255, 255, 255, 0.2);
		padding: 10rpx 20rpx;
		border-radius: 30rpx;
		cursor: pointer;
	}

	.withdraw-record text {
		color: #fff;
		font-size: 24rpx;
		margin-left: 8rpx;
	}

	.amount-options {
		display: flex;
		flex-wrap: wrap;
		margin: 0 -10rpx 30rpx;
	}

	.amount-item {
		position: relative;
		width: calc(33.33% - 20rpx);
		margin: 10rpx;
		height: 70rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #f7f8fa;
		border-radius: 8rpx;
		transition: all 0.3s;
		border: 1rpx solid transparent;
		box-sizing: border-box;
	}

	.amount-item text {
		font-size: 28rpx;
		color: #333;
		transition: all 0.3s;
	}

	.amount-item.active {
		background: rgba(30, 199, 150, 0.1);
		border-color: #1ec796;
	}

	.amount-item.active text {
		color: #1ec796;
		font-weight: 500;
	}

	.amount-item:active {
		opacity: 0.9;
		transform: scale(0.98);
	}

	.not-login {
		display: flex;
		align-items: center;
		justify-content: center;
		min-height: 300rpx;
	}

	.login-tip {
		text-align: center;
		padding: 40rpx 0;
	}

	.login-tip text {
		display: block;
		font-size: 28rpx;
		color: #666;
		margin-bottom: 30rpx;
	}

	.login-btn {
		width: 240rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: #1ec796;
		color: #fff;
		font-size: 28rpx;
		border-radius: 40rpx;
		border: none;
	}

	.login-btn:active {
		opacity: 0.9;
	}
</style>